package com.example.streamdemo.groupingBy;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;

public class GroupingByTest {
    public static void main(String[] args) {
        ArrayList<MyStudent> myStudents = new ArrayList<>();
        myStudents.add(new MyStudent().setStuName("学生1").setMyClass(new MyClass().setClassName("301")));
        myStudents.add(new MyStudent().setStuName("学生2").setMyClass(new MyClass().setClassName("301")));
        myStudents.add(new MyStudent().setStuName("学生3").setMyClass(new MyClass().setClassName("302")));
        myStudents.add(new MyStudent().setStuName("学生4").setMyClass(new MyClass().setClassName("303")));
        myStudents.add(new MyStudent().setStuName("学生5").setMyClass(new MyClass().setClassName("301")));
        myStudents.add(new MyStudent().setStuName("学生6").setMyClass(new MyClass().setClassName("301")));
        Map<String, List<MyStudent>> collect = myStudents.stream().collect(Collectors.groupingBy(e -> e.getMyClass().getClassName()));
        collect.forEach((a, b) -> {
            System.out.println(a + " " + b);
        });
        Long collect1 = collect.entrySet().stream().collect(Collectors.counting());

        System.out.println("collect1 "+collect1);
        myStudents.forEach(((Consumer<MyStudent>) myStudent -> {
            System.out.print(myStudent.getStuName() + " ");
        }).andThen(e -> System.out.println(e.getMyClass().getClassName())));

        fun1(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) {
                System.out.println("accept"+integer);
            }
        }.andThen((e)-> System.out.println("andThen")),1);
    }

    public static <T> void fun1(Consumer<T> c,T t) {
//        Consumer<T> tConsumer = c.andThen(c);
        c.accept(t);
    }
}

@Data
@Accessors(chain = true)
class MyClass {
    private String className;
}

@Data
@Accessors(chain = true)
class MyStudent {
    private String stuName;
    private MyClass myClass;
}
